package com.huluobo.h3kotlinproject.register

import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.h3kotlinproject.R
import com.huluobo.h3kotlinproject.base.BaseMVVMActivity
import com.huluobo.h3kotlinproject.databinding.ActivityRegisterBinding

class RegisterActivity : BaseMVVMActivity<ActivityRegisterBinding>() {

    private lateinit var viewModel: RegisterViewModel
    override fun initData() {
        viewModel = ViewModelProvider(this)[RegisterViewModel::class.java]

        viewModel.success.observe(this) {
            if (it.code == 500) {
                ToastUtils.showLong("用户已存在")
            } else {
                ToastUtils.showLong("注册成功:" + it.data.username)
                finish()
            }
        }

        viewModel.failed.observe(this) {
            Log.i("TagA", "e:$it")
        }
    }

    override fun initView() {
        dataBinding.registerBtn.setOnClickListener {
            viewModel.register(
                dataBinding.registerEtUsername.text.toString(),
                dataBinding.registerEtPassword.text.toString(),
                dataBinding.registerEtRePassword.text.toString(),
            )
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_register
}